'''
    Goblinoid: Experience all of MediaGoblin on an Android Device
    Copyright (C) 2015 Dylan Jeffers

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
'''
from __future__ import print_function

from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.screenmanager import Screen
from kivy.uix.togglebutton import ToggleButton

from libs.toast.kivytoast import toast

Builder.load_file('mediaviews.kv')


class UploadView(Screen):
    upload_image = StringProperty('')

    def init_screen(self):
        print(self.manager.media_model.upload_image)
        self.upload_image = self.manager.media_model.upload_image

    def upload_media(self):
        pump = App.get_running_app().pump_model.pump
        title = self.ids.title.text
        description = self.ids.desc.text
        to = self.ids.to.text
        cc = self.ids.cc.text

        material = [title, description, to, cc]
        print(material)

        image = pump.Image(display_name=title)
        image.content = description
        image.to = pump.Person(to)
        # for now will only do public
        image.cc = pump.Public
        image.from_file(self.upload_image)
        toast('media uploaded', True)
        self.manager.parent.parent.open_media_view('gallery', True)


class GalleryView(Screen):
    media_model = ObjectProperty(None)
    gallery = ObjectProperty(None)
    image_grid = ObjectProperty(None)
    highlighted_image = StringProperty('')

    def __init__(self, **kwargs):
        super(GalleryView, self).__init__(**kwargs)
        self.image_grid.bind(minimum_height=self.image_grid.setter('height'))

    def init_screen(self):
        pass

    def on_enter(self):
        for image in self.manager.media_model.gallery_content:
            toggle = ToggleButton(
                group='gallery',
                background_normal=image,
                # still need to figure out on_pressed background
                )
            toggle.bind(on_release=self.highlighted_image)
            self.image_grid.add_widget(toggle)

    def highlighted_image(self, image):
        print (image.background_normal)
        self.highlighted_image = image.background_normal


class CameraView(Screen):

    def init_screen(self):
        pass


class CamcorderView(Screen):

    def init_screen(self):
        pass
